<?php
class CmsController extends AppController {

	var $name = 'Cms';
	var $cms_section_files = array('CmsSection.id','CmsSection.cms_id','CmsSection.body','CmsSection.status');
	var $cms_files = array('Cm.id','Cm.controller','Cm.action','Cm.name','Cm.keywords','Cm.description');	
	var $paginate = array(
        'fields' => array('Cm.id','Cm.controller','Cm.action','Cm.name','Cm.keywords','Cm.description'),
        'limit' => 25,        
        'order' => array(
            'Cm.name' => 'asc'
        )
    );
	var $permissions = array(
		'add' => array('admin'),
		'index' => array('admin'),
		'view' => array('admin','manag'),
		'edit' => array('admin','manag'),
		'admin_edit' => array('admin'),
		'delete' => array('admin')
	);    
    
	public function beforeFilter(){
		parent::beforeFilter();		
	}
	public function beforeRender(){
		parent::beforeRender();
		
		$this->layout = 'cms';
	}
	
	function index() {
		$this->Cm->recursive = 0;
		$this->set('cms', $this->paginate());
	}

	function view($id = null) {
		if (!$id) {
			$this->Session->setFlash(__('Invalid cm', true));
			$this->redirect(array('action' => 'index'));
		}
		$this->set('cm', $this->Cm->read($this->cms_files, $id));
	}

	function add() {
		if (!empty($this->data)) {
			$this->Cm->create();
			if ($this->Cm->save($this->data)) {
				$this->Session->setFlash(__('The cm has been saved', true));
				$this->redirect(array('action' => 'index'));
			} else {
				$this->Session->setFlash(__('The cm could not be saved. Please, try again.', true));
			}
		}
//		$cmsSections = $this->Cm->CmsSection->find('list');
//		$this->set(compact('cmsSections'));
	}
	
	function edit($id = null) {
		if (!$id && empty($this->data)) {
			$this->Session->setFlash(__('Invalid cm', true));
			$this->redirect($this->referer());
		}
		if (!empty($this->data)) {			
			if ($this->Cm->save($this->data)) {
				$this->Session->setFlash(__('The cm has been saved', true));
				$this->redirect($this->referer());
			} else {
				$this->Session->setFlash(__('The cm could not be saved. Please, try again.', true));
			}
		}
		if (empty($this->data)) {
			$this->data = $this->Cm->read($this->cms_files, $id);
		}
		$cmsSections = $this->Cm->CmsSection->find('list');
		$this->set(compact('cmsSections'));
	}

	function admin_edit($id = null) {
		if (!$id && empty($this->data)) {
			$this->Session->setFlash(__('Invalid cm', true));
			$this->redirect(array('action' => 'index'));
		}
		if (!empty($this->data)) {			
			if ($this->Cm->save($this->data)) {
				$this->Session->setFlash(__('The cm has been saved', true));
				$this->redirect(array('action' => 'index'));
			} else {
				$this->Session->setFlash(__('The cm could not be saved. Please, try again.', true));
			}
		}
		if (empty($this->data)) {
			$this->data = $this->Cm->read($this->cms_files, $id);
		}
		$cmsSections = $this->Cm->CmsSection->find('list');
		$this->set(compact('cmsSections'));
		$this->render('edit');
	}

	function delete($id = null) {
		if (!$id) {
			$this->Session->setFlash(__('Invalid id for cm', true));
			$this->redirect(array('action'=>'index'));
		}
		if ($this->Cm->delete($id)) {
			$this->Session->setFlash(__('Cm deleted', true));
			$this->redirect(array('action'=>'index'));
		}
		$this->Session->setFlash(__('Cm was not deleted', true));
		$this->redirect(array('action' => 'index'));
	}
}
?>